<!--
 * @Description:
 * @version:
 * @Author: sunqian_sr
 * @Date: 2022-12-01 18:24:20
 * @LastEditors: Andy
 * @LastEditTime: 2023-02-21 16:37:41
-->
<template>
  <x-dialog :title="title" :show.sync="isVisible" v-if="isVisible" :before-close="handleClose" width="35%">
    <el-form :model="params" class="demo-form-inline" ref="params" :rules="rules" label-position="top">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="'组织机构'" prop="orgId">
            <x-select-tree
              v-model="params.orgId"
              :options="orgTreeData"
              :placeholder="'根节点'"
              :defaultProps="defaultOrgProps"
            />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
          <el-form-item :label="'岗位'" prop="postId">
            <x-select-tree v-model="params.postId" :options="postTreeData" :defaultProps="defaultPostProps" />
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item :label="`角色类型`" prop="roleType">
            <x-select
              :placeholder="'请选择'"
              v-model="params.roleType"
              :options="dictOptions['upms_role.role_type']"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="`角色名称`" prop="roleName">
            <el-input v-model="params.roleName" :placeholder="'请输入'" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="`角色编码`" prop="roleCode">
            <el-input v-model="params.roleCode" :placeholder="'请输入'" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="'是否启用'" prop="state">
            <x-radio-group :list-data="dictOptions['system.state']" v-model="params.state"></x-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="'备注'" prop="remark">
            <el-input type="textarea" v-model="params.remark" style="width: 100%"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button @click="handleClose"> {{ $t('common.cancel') }} </el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading"> {{ $t('common.confirm') }} </el-button>
    </span>
  </x-dialog>
</template>
<script>
  import { selectDictLabel, cloneDeep, omit, throttle } from '@package/utils'
  import { apiPostAddRole, apiPostEditRole } from '../api'
  import { apiPostOrganizTree, apiPostTree } from '../../organization/api'

  import { EnumApiCode } from '../../../../../../globalEnum'

  export default {
    components: {},
    props: {
      dictOptions: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      return {
        title: '新增菜单',
        isVisible: false,
        loading: false,
        params: {
          orgId: '',
          roleType: '',
          roleName: '',
          roleCode: '',
          sort: 1,
          state: '1',
          remark: ''
          // postId: ''
        },
        rules: {
          roleName: [
            { required: true, message: '请输入角色名称', trigger: 'blur' },
            { min: 1, max: 30, message: '长度在 1 到 30 个字符 ', trigger: 'blur' }
          ],
          roleType: [{ required: true, message: '请选择角色类型', trigger: 'blur' }],
          roleCode: [{ required: true, message: '请输入权限字符', trigger: 'blur' }],
          orgId: [{ required: true, message: '请选择组织机构', trigger: 'blur' }]
        },
        orgTreeData: [],
        defaultOrgProps: {
          label: 'orgName',
          children: 'children'
        },
        postTreeData: [],
        defaultPostProps: {
          label: 'postName',
          children: 'children'
        }
      }
    },
    mounted() {
      this.loadTreeData()
      this.loadPostTreeData()
    },
    methods: {
      handleOpen(info) {
        this.isVisible = true
        if (info?.id) {
          this.title = '编辑角色'
          this.$nextTick(() => {
            this.params = Object.assign(this.params, info)
            this.params.dsType += ''
            this.params.state += ''
          })
          return
        }
        this.title = '新增角色'
      },
      async loadTreeData() {
        const { data, code, msg } = await apiPostOrganizTree()
        if (code === EnumApiCode.Success) {
          this.orgTreeData = data
        } else {
          this.$message.error(msg)
        }
      },
      loadPostTreeData() {
        apiPostTree().then(({ code, data, msg }) => {
          if (code === EnumApiCode.Success) {
            this.postTreeData = data
          } else {
            this.$message.error(msg)
          }
        })
      },
      handleSubmit: throttle(function () {
        this.loading = true
        this.$refs.params.validate(async (valid) => {
          if (valid) {
            const param = {
              ...this.params,
              orgId: typeof this.params.orgId === 'object' ? this.params.orgId.value : this.params.orgId
            }
            try {
              const { code, msg } = 'id' in this.params ? await apiPostEditRole(param) : await apiPostAddRole(param)
              this.loading = false
              if (code === EnumApiCode.Success) {
                this.$message.success(param.id ? this.$t('common.editSuccess') : this.$t('common.newSuccess'))
                this.$emit('handleSuccess')
                this.handleClose()
              } else {
                this.$message.error(msg)
              }
            } finally {
              this.loading = false
            }
          } else {
            return false
          }
        })
      }, 1000),
      handleClose() {
        this.$refs.params.resetFields()
        this.params = omit(this.params, 'id')
        this.isVisible = false
      }
    }
  }
</script>
